package com.heima.article.controller;

import com.heima.article.dto.ArticleDto;
import com.heima.article.dto.ArticleHomeDto;
import com.heima.article.dto.ArticleInfoDto;
import com.heima.article.entity.ApCollection;
import com.heima.article.service.IApArticleService;
import com.heima.article.service.IApAuthorService;
import com.heima.article.service.IApCollectionService;
import com.heima.behavior.dto.ArticleBehaviorDto;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.entity.ApFollowBehavior;
import com.heima.behavior.entity.ApLikesBehavior;
import com.heima.behavior.entity.ApUnlikesBehavior;
import com.heima.common.Threadlocals.UserThreadLocalUtil;
import com.heima.article.dto.CollectionBehaviorDto;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.feign.behavior.BehaviorFeign;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文章信息表，存储已发布的文章 前端控制器
 * </p>
 *
 * @author itheima
 * @since 2022-07-03
 */
@RestController
@RequestMapping("/api/v1/article")
@Api(tags = "文章信息表，存储已发布的文章接口")
@CrossOrigin
public class ApArticleController{

    @Autowired
    private IApArticleService apArticleService;

    @Autowired
    private IApCollectionService apCollectionService;
    @Autowired
    private IApAuthorService apAuthorService;

    @Autowired
    private BehaviorFeign behaviorFeign;


    /**
     * 加载文章行为-数据回显
     * @param articleBehaviorDto
     * @return
     */
    @PostMapping("/load_article_behavior")
    public ResponseResult loadArticleBehavior(@RequestBody ArticleBehaviorDto articleBehaviorDto){
        //0.创建返回集合
        Map<String,Boolean> ResultMap = new HashMap<>();

        //1.根据作者ID查询用户ID
        //此处用户ID应为当前登录设备的用户
        User user = UserThreadLocalUtil.get();
        Integer userId = user.getUserId();

        //2.根据用户ID和设备ID获取实体ID
        ApBehaviorEntry entry = behaviorFeign.findEntryByUesridAndEqid(userId, articleBehaviorDto.getEquipmentId());
        Integer entryId = Integer.parseInt(entry.getEntryId());

        //3.根据实体ID查询是否关注   操作类型: 0 关注 1 取消关注
        ApFollowBehavior followBehavior = behaviorFeign.findFollowBehavior(entryId);
        Integer operation = followBehavior.getOperation();
        ResultMap.put("isfollow", (operation ==0?true:false) );

        //4.根据实体ID查询是否喜欢    0 点赞  1 取消点赞',
        ApLikesBehavior apLikesBehavior = behaviorFeign.findApLikesBehavior(articleBehaviorDto.getArticleId(), entryId);
        Integer likesBehaviorOperation = apLikesBehavior.getOperation();
        ResultMap.put("islike", (likesBehaviorOperation ==0?true:false) );

        //5.根据实体ID查询是否不喜欢    0 不喜欢  1 取消不喜欢',
        ApUnlikesBehavior apUnlikesBehavior = behaviorFeign.findApUnlikesBehavior(articleBehaviorDto.getArticleId(), entryId);
        Integer type = apUnlikesBehavior.getType();
        ResultMap.put("isunlike", (type ==0?true:false) );

        //6.根据实体ID查询是否收藏    0 不喜欢  1 取消不喜欢'
        ApCollection apCollection = apCollectionService.findByArticleidAndEntyrid(articleBehaviorDto.getArticleId(), entryId);
        ResultMap.put("iscollection", (apCollection !=null?true:false) );

        return ResponseResult.okResult(ResultMap);

    }

    /**
     * 发布正式文章
     * @param dto 文章信息
     * @return
     */
    @PostMapping
    public ResponseResult<Long> saveArticle(@RequestBody ArticleDto dto){
        return ResponseResult.okResult(apArticleService.saveArticle(dto));
    }


    /**
     * 首次进入首页：加载文章列表
     *
     * @param dto
     * @return
     */
    @PostMapping("/load")
    public ResponseResult loadArticle(@RequestBody ArticleHomeDto dto) {
        return apArticleService.loadArticle(dto, 0);
    }

    /**
     * 上拉获取更多文章列表
     *
     * @param dto
     * @return
     */
    @PostMapping("/loadmore")
    public ResponseResult loadMore(@RequestBody ArticleHomeDto dto) {
        return apArticleService.loadArticle(dto, 1);
    }

    /***
     * 下拉获取最新文章列表
     * @param dto
     * @return
     */
    @PostMapping("/loadnew")
    public ResponseResult loadNew(@RequestBody ArticleHomeDto dto) {
        return apArticleService.loadArticle(dto, 0);
    }


    /**
     * 查询文章详情
     * @param dto
     * @return
     */
    @PostMapping("/load_article_info")
    public ResponseResult loadArticleInfo(@RequestBody ArticleInfoDto dto){
        return apArticleService.loadArticleInfo(dto);
    }

    /**
     * 文章收藏&取消收藏接口
     * @param dto
     * @return
     */
    @PostMapping("/collection")
    public ResponseResult collectionArticle(@RequestBody CollectionBehaviorDto dto){
        return apCollectionService.collectionArticle(dto);
    }

}
